package com.study.quiz.controller;

import com.study.quiz.dto.NotifictionDTO;
import com.study.quiz.enums.NotifictionEnum;
import com.study.quiz.pojo.User;
import com.study.quiz.service.NotifictionService;
import com.study.quiz.utils.ErrorIndexUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author CJunGod
 * @create 2020-03-14 11:51
 **/
@Controller
public class NotifictionController {

    @Resource
    private NotifictionService notifictionService;

    @Resource
    private ErrorIndexUtil errorIndexUtil;

    @GetMapping("/notification/{id}")
    public String getNotification(@PathVariable("id") Long notificationId,
                                  HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            errorIndexUtil.errorIndex("用户未登录，请登录后重试");
            return "redirect:/";
        }

        NotifictionDTO notifictionDTO = notifictionService.read(notificationId, user);
        if(notifictionDTO == null){
            errorIndexUtil.errorIndex("非法读取用户信息");
        }

        if(NotifictionEnum.REPLY_COMMENT.getType() == notifictionDTO.getType()
                || NotifictionEnum.REPLY_QUESTION.getType() == notifictionDTO.getType()){

            return "redirect:/question/"+notifictionDTO.getOuterId();
        }else{
            return "redirect:/";
        }
    }
}
